<template>
  <div v-if="following.length">
    <UserFollowItem :user="user" @onclick_follow="onclick_follow" v-for="(user,index) in following"
                    :key="index"></UserFollowItem>
  </div>

</template>

<script>
  import UserFollowItem from "../../../components/UserFollowItem";

  export default {
    props: ["userId"],
    components: {
      UserFollowItem
    },
    data() {
      return {
        following: [],
      }
    },
    created() {
      this.getFollowing()
    },
    methods: {
      // 获取关注的人
      async getFollowing() {
        let res = await this.$http.get("/user/" + this.userId + "/following")
        if (res.code == 0) {
          this.following = res.data;
          console.log("following", this.following)
        }
      },
      // 点击关注或者取消关注
      async onclick_follow(userId, isFollowed) {
        console.log(userId, isFollowed)
        let res
        if (isFollowed) {
          res = await this.$http.delete("/user/follow/" + userId);

        } else {
          res = await this.$http.put("/user/follow/" + userId);
        }
        if (res.code == 0) {
          this.$store.dispatch("detail");
        }
      },
    }
  }
</script>

<style scoped>

</style>
